package com.commonly.commonlyUsed.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class StringUtils {

    public StringUtils() {
    }

    public static boolean isNullOrEmpty(String text) {
        return text == null || text.length() == 0;
    }

    public static String objectToJson(Object obj) {
        return JSON.toJSONString(obj);
    }

    public static <T> T jsonToObjcet(String json, Class<T> valueType) {
        try {
            return JSONObject.parseObject(json, valueType, new Feature[]{Feature.AllowISO8601DateFormat});
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static <T> T jsonToObjcet(String json, TypeReference<T> typeReference) {
        try {
            return JSONObject.parseObject(json, typeReference, new Feature[]{Feature.AllowISO8601DateFormat});
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static <T> T jsonToObjcet(String json, Type type) {
        try {
            return JSONObject.parseObject(json, type, new Feature[]{Feature.AllowISO8601DateFormat});
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static JSONObject jsonToJSONObject(String json) {
        try {
            JSONObject result = JSONObject.parseObject(json);
            return result;
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static <T> T jsonToObjcet(String json) {
        try {
            return JSONObject.parseObject(json, new TypeReference<T>() {
            }, new Feature[]{Feature.AllowISO8601DateFormat});
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static <T> List<T> jsonToList(String jsonStr, Class<T> cls) {
        return JSONObject.parseArray(jsonStr, cls);
    }

    public static <T> List<T> jsonToList(String jsonStr) {
        return (List) JSON.parseObject(jsonStr, new TypeReference<List<T>>() {
        }, new Feature[]{Feature.AllowISO8601DateFormat});
    }

    public static <T> T parserXML(String xml) {
        ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
        XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(in));
        decoder.close();
        return (T) decoder.readObject();
    }

    public static <T> String formatXML(T entity) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(out));
        encoder.writeObject(entity);
        encoder.close();
        return out.toString();
    }

    public static final String inputStream2String(InputStream in) throws UnsupportedEncodingException, IOException {
        if (in == null) {
            return "";
        } else {
            StringBuffer out = new StringBuffer();
            InputStreamReader isr = new InputStreamReader(in, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            String line = null;

            while ((line = br.readLine()) != null) {
                out.append(line);
            }

            return out.toString();
        }
    }

    public static String trimEnd(String p_String) {
        return trimEnd(p_String, (String) null);
    }

    public static String trimEnd(String p_String, String p_TrimChars) {
        if (isNullOrEmpty(p_TrimChars)) {
            p_TrimChars = "\\s";
        }

        return p_String.replaceAll("[" + p_TrimChars + "]+$", "");
    }

    public static String trimStart(String text) {
        return trimStart(text, (String) null);
    }

    public static String trimStart(String text, String trimChars) {
        if (isNullOrEmpty(trimChars)) {
            trimChars = "\\s";
        }

        return text.replaceAll("^[" + trimChars + "]+", "");
    }

    public static String trim(String p_String) {
        return p_String.trim();
    }

    public static <T> String stringJoin(Collection<T> collection, String separator) {
        StringBuilder sb = new StringBuilder();
        Iterator var3 = collection.iterator();

        while (var3.hasNext()) {
            T item = (T) var3.next();
            sb.append(MessageFormat.format("{0}{1}", item.toString(), separator));
        }

        return sb.toString();
    }
}
